package model;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 * 
 * @author Lars
 *
 */

@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@Table(name="Delbehandlinger")
public abstract class DelBehandling {
	@Id
	@GeneratedValue(strategy=GenerationType.SEQUENCE)
	private int id;
	
	private int sort_order;
	
	@ManyToOne(cascade=CascadeType.ALL)
	private Behandling behandling;
	
	@OneToMany(mappedBy="delBehandling", cascade=CascadeType.ALL)
	private List<TimeStamp> timeStamps;
	
	public DelBehandling() {
		this.timeStamps = new ArrayList<TimeStamp>();
	}
	
	public void setSortOrder(int sort_order) {
		this.sort_order = sort_order;
	}
	
	public Behandling getBehandling() {
		return this.behandling;
	}
	
	public void setBehandling(Behandling behandling) {
		setBehandlingUD(behandling);
		behandling.addDelBehandlingUD(this);
	}
	
	protected void setBehandlingUD(Behandling behandling) {
		this.behandling = behandling;
	}
	
	public void removeBehandling() {
		this.behandling = null;
	}
	
	private void addTimeStampUD(TimeStamp timeStamp) {
		timeStamps.add(timeStamp);
	}
	
	private void removeTimeStampUD(TimeStamp timestamp){
		timeStamps.remove(timestamp);
	}
	
	public void addTimeStamp(TimeStamp timestamp){
		this.addTimeStampUD(timestamp);
		timestamp.setDelBehandlingUD(this);
	}
	
	public void removeTimeStamp(TimeStamp timestamp){
		timestamp.setDelBehandlingUD(null);
		this.removeTimeStampUD(timestamp);
	}
	
	public TimeStamp getNewestTimeStamp() {
		return timeStamps.get(timeStamps.size()-1);
	}
	
	public ArrayList<TimeStamp> getTimeStamps(){
		return new ArrayList<TimeStamp>(timeStamps);
	}
	
	public abstract PlukkeklarEnum getPlukkeklar();
	public abstract long getTimeLeftInMilis();
	public abstract String toString();
	public abstract DelBehandling copy();
}
